package com.coszero.common.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.coszero.common.widget.MyToolBar;
import com.coszero.utils.utils.LogX;

import org.greenrobot.eventbus.EventBus;

/**
 * Desc：
 *
 * @author xmqian
 * Email:xmqian93@163.com
 * Date: 2020/6/9 14:37
 * @version 2
 */
public abstract class BaseFragment extends Fragment {
    @Nullable
    MyToolBar mMyToolbar;
    protected String TAG = this.getClass().getSimpleName();
    private static final long CLICK_INTERVAL = 500; // 防重复点击的时间间隔，单位毫秒
    private long lastClickTime = 0; // 记录上一次点击的时间戳
    protected Activity mActivity;
    protected Context mContext;
    /**
     * 用来重置绑定
     */
    private boolean isDataLoaded;

    /**
     * 是否已经初始化成功数据
     *
     * @param dataLoaded true 不再触发懒加载 false触发懒加载
     */
    public void setDataLoaded(boolean dataLoaded) {
        isDataLoaded = dataLoaded;
    }

    // <editor-fold desc="生命周期" defaultstate="collapsed">

    @Override
    public void onAttach(@NonNull Context context) {
        mActivity = (Activity) context;
        mContext = context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //        try {
        return getLayoutView(inflater, container, false);
//        } catch (Exception e) {
//            String errorMsg = "布局使用异常，移除类：" + this.getClass().getSimpleName() + " 异常布局ID:" + getLayoutResource() +
//                    " 异常信息：" + e.toString();
//            LogX.e("###BaseMyFragment", errorMsg);
//            new Throwable(errorMsg);
//        }
//        return null;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        try {
            initView();
            initPresenter();
            initData();
        } catch (Exception e) {
            LogX.e("### BaseFragment", "初始化失败:" + e.getMessage());
        }
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }

    //Pager会调用的
    @Override
    @SuppressWarnings({"unchecked", "deprecation"})
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        getLazyData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getLazyData();
        }
    }
    //</editor-fold>

    public void getLazyData() {
        if (getView() != null) {
            if (!isDataLoaded) {
                LogX.d("###", "懒加载");
                lazyLoadData();
                isDataLoaded = true;
            }
        }
    }

    public boolean isDataLoaded() {
        return isDataLoaded;
    }


    protected void setTitle(int resId) {
        setTitle(getResources().getString(resId));
    }

    protected void setTitle(String text) {
        if (getmMyToolbar() != null) {
            mMyToolbar.setMainTitle(text);
        }
    }

    /**
     * 防重复点击方法
     *
     * @return 如果可以点击，返回true；否则返回false
     */
    protected boolean isClickButtonInterval() {
        long currentTime = SystemClock.elapsedRealtime();
        if (currentTime - lastClickTime > CLICK_INTERVAL) {
            lastClickTime = currentTime;
            return true;
        }
        return false;
    }

    @Nullable
    public MyToolBar getmMyToolbar() {
        return mMyToolbar;
    }

    protected abstract void initView();


    /**
     * 提供给MVP模式使用
     */
    protected void initPresenter() {
    }

    protected abstract void initData();

    /*懒加载封装方法*/
    public abstract void lazyLoadData();

    /**
     * 使用viewBinding或dataBinding时，必须重写此方法，返回布局
     *
     * @param inflater inflater
     * @param container container
     * @param attachToParent attachToParent
     * @return view
     */
    public abstract View getLayoutView(LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToParent);
}
